package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.BabyFootBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class BabyFootAdapter extends RecyclerView.Adapter<BabyFootAdapter.MyHolder> {

    private Context mContext;
    private List<BabyFootBean.DataBean> list = null;
    private OnLister lister;

    public BabyFootAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_baby_foot, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        BabyFootBean.DataBean bean = list.get(position);
        Glide.with(mContext)
                .load(bean.getImg())
                .bitmapTransform(new CenterCrop(mContext),new GlideRoundTransform(mContext,10))
                .into(viewHolder.imgFoot);
        viewHolder.tvFootType.setText(bean.getMealTimes());
        viewHolder.tvFootContent.setText(bean.getFood());
        viewHolder.tvFootTime.setText(bean.getDay());
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<BabyFootBean.DataBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<BabyFootBean.DataBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgFoot;
        public TextView tvFootType;
        public TextView tvFootTime;
        public TextView tvFootContent;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgFoot = itemView.findViewById(R.id.img_foot);
            tvFootType = itemView.findViewById(R.id.tv_foot_type);
            tvFootTime = itemView.findViewById(R.id.tv_foot_time);
            tvFootContent = itemView.findViewById(R.id.tv_foot_content);
        }
    }

    public interface OnLister {
        void lister(int position);
    }
}